package Java道题.狼羊白菜问题;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @description:
 * @author: ${ywk}
 * @date: 2020-05-17
 */
public class 狼羊白菜问题1 {
    public static void main(String[] args) {
        int arr[] = {0, 0, 0, 0};
        ArrayList<String> strings = new ArrayList<>();
        strings.add(Arrays.toString(arr));
        solution(arr, strings);

    }

    public static void solution(int[] arr, List<String> path) {

        for (int i = 0; i < arr.length - 1; i++) {

            if (arr[arr.length - 1] == 1) {
                arr[arr.length - 1] = 0;
                for (int k = 0; k < arr.length - 1; k++) {
                    if (arr[k] == 1) {
                        if (isValid(arr, path)) {

                            path.add(Arrays.toString(arr));
                            solution(arr, path);
                            path.remove(path.size() - 1);

                        }
                        arr[k] = 0;
                        if (isValid(arr, path)) {
                            path.add(Arrays.toString(arr));
                            solution(arr, path);
                            path.remove(path.size() - 1);

                        } else {
                            arr[k] = 1;
                        }

                    }
                }
            } else {
                if (arr[i] == 1) {
                    continue;
                }
                arr[arr.length - 1] = 1;
                arr[i] = 1;
                if (!isValid(arr, path)) {
                    arr[arr.length - 1] = 0;
                    arr[i] = 0;
                    continue;
                }
                path.add(Arrays.toString(arr));

                if (isEnd(arr)) {
                    System.out.println(Arrays.toString(path.toArray()));
                    return;
                }
                solution(arr, path);
                path.remove(path.size() - 1);
                arr[i] = 0;
            }
        }

    }


    public static boolean isEnd(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != 1) {
                return false;
            }
        }
        return true;
    }


    public static boolean isValid(int[] arr, List path) {
        int com = 0;
        if (arr[arr.length - 1] == 0) {
            com = 1;
        }
        for (int i = 0; i < arr.length - 2; i++) {
            if (arr[i] == arr[i + 1] && arr[i] == com) {
                return false;
            }

        }
        return path.indexOf(Arrays.toString(arr)) == -1 ? true : false;
    }
}
